package com.grow.greenhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.grow.greenhouse.common.ErrorCode;
import com.grow.greenhouse.domain.Shed;
import com.grow.greenhouse.domain.User;
import com.grow.greenhouse.domain.request.ShedNameRequest;
import com.grow.greenhouse.exception.BusinessException;
import com.grow.greenhouse.job.MQListener;
import com.grow.greenhouse.once.UserContext;
import com.grow.greenhouse.service.ShedService;
import com.grow.greenhouse.mapper.ShedMapper;
import com.grow.greenhouse.service.UserService;
import com.grow.greenhouse.utils.JwtUtil;
import com.grow.greenhouse.utils.QiNiuCloudUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author 23854
* @description 针对表【shed】的数据库操作Service实现
* @createDate 2024-01-29 12:34:23
*/
@Service
public class ShedServiceImpl extends ServiceImpl<ShedMapper, Shed>
    implements ShedService{

    @Resource
    private Gson gson;

    @Resource
    private UserService userService;
    @Override
    public String chooseShed(String shedName, HttpServletRequest request) {

        long userId = Long.parseLong(JwtUtil.getUserIdFromToken(request.getHeader("token")));
        User loginUser = userService.getLoginUser(request);
        String shedNames = loginUser.getShedName();
        List<String> shedList = gson.fromJson(shedNames, new TypeToken<List<String>>() {
        }.getType());
        if(!shedList.contains(shedName)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"该用户没有此大棚");
        }
        UserContext.setUserId(userId);
        UserContext.setShedName(shedName);

        return "修改棚名称成功";
    }

    @Override
    public List<Shed> getUserShed(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        String shedNameJson = loginUser.getShedName();
        List<String> shedNameList = gson.fromJson(shedNameJson, new TypeToken<List<String>>() {
        }.getType());
        QueryWrapper<Shed> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("shedName",shedNameList);
        List<Shed> shedList = new ArrayList<>();
        for(String shedName : shedNameList)
        {
            queryWrapper.like(shedName,shedNameList);
            shedList.add(this.getOne(queryWrapper));
        }
        return shedList;
    }

    @Override
    public String addShed(ShedNameRequest shedNameRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        String shedName = shedNameRequest.getShedName();
        String shedNameJson = loginUser.getShedName();
        List<String> shedList = gson.fromJson(shedNameJson, new TypeToken<List<String>>() {
        }.getType());
        if(shedList.contains(shedName))
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户已拥有此大棚，请重新选择大棚名称");
        }
        shedList.add(shedName);
        String json = gson.toJson(shedList);
        loginUser.setShedName(json);
        boolean update = userService.updateById(loginUser);
        if(!update){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"新增大棚失败");
        }
        return "新增大棚成功";

    }
}




